<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DataTable
 *
 * @author QuangKhoi
 */
class DataTable {
    //put your code here
    
    private $numOfRow = null;
    private $rows = null;
    private $queryResult = null;
    
    public function __construct($queryResult) {
        $this->queryResult = $queryResult;
    }
    
    /**
     * 
     * @param type $name
     * @return int|null|rows
     */
    public function __get($name) {
        switch($name) {
            case "numOfRow":
                if ($this->numOfRow == null) {
                    //get the num of rows from query result
                    if ($this->queryResult != null)
                        return mysql_num_rows($this->queryResult);
                    else
                        return 0;
                }
                else 
                    return $this->numOfRow;
                break;
            case "rows":
                if ($this->rows == null) {
                    //get the array of rows
                    if ($this->queryResult != null) {
                        $rows_temp = array();
                        while($row = mysql_fetch_array($this->queryResult)) {
                            $rows_temp[] = $row;
                        }
                        $this->rows = $rows_temp;
                        return $rows_temp;
                    }
                    else
                        return array(); //Return empty array if there's no query result
                }
                else 
                    return $this->rows;
                break;
            default:
                //Default return nothing
                return null;
                break;
        }
    }
}

?>
